package com.example.demo.service.impl;

import com.example.demo.domain.props.MinioProp;
import com.example.demo.service.CommService;
import io.minio.*;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


/**
 * @Author: 聂吉泽
 * @Description:
 * @Date: create in 2021/11/18 11:04
 */
@Service
public class CommServiceImpl implements CommService {

    @Resource
    private MinioClient minioClient;

    @Resource
    private MinioProp minioProp;

    @Override
    public void upload(MultipartFile file) throws Exception {
        PutObjectArgs objectArgs = PutObjectArgs.builder().object(file.getOriginalFilename())
                .bucket(minioProp.getBucket())
                .contentType(file.getContentType())
                .stream(file.getInputStream(),file.getSize(),-1).build();
        minioClient.putObject(objectArgs);
    }

    @Override
    public void download(String fileName, HttpServletResponse response) throws Exception {
        GetObjectArgs build = GetObjectArgs.builder().bucket(minioProp.getBucket()).object(fileName).build();
        InputStream in = minioClient.getObject(build);
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/octet-stream");
        IOUtils.copy(in,response.getOutputStream());
    }

    @Override
    public void downloadZip(String downName,HttpServletResponse response) throws Exception {
        List<String> fileList=new ArrayList<>();
        fileList.add("test1.xls");
        fileList.add("test2.png");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(downName, "UTF-8"));
        OutputStream outputStream = response.getOutputStream();
        ZipOutputStream zos=new ZipOutputStream(outputStream);
        for(String s:fileList){
            ZipEntry entry = new ZipEntry(s);
            InputStream is = null;
            BufferedInputStream in = null;
            byte[] buffer = new byte[1024];
            int len;
            try{
                GetObjectArgs build = GetObjectArgs.builder().bucket(minioProp.getBucket()).object(s).build();
                is = minioClient.getObject(build);
                in = new BufferedInputStream(is);
                zos.putNextEntry(entry);
                //文件流循环写入ZipOutputStream
                while ((len = in.read(buffer)) != -1 ) {
                    zos.write(buffer, 0, len);
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally{
                try{
                    if(entry != null) {
                        zos.closeEntry();
                    }
                    if(in != null) {
                        in.close();
                    }
                    if(is != null) {
                        is.close();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        try{
            if(zos != null) {
                zos.close();
            }
            if(outputStream != null) {
                outputStream.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public void copyObject(String backupBucket,String newName, String oldBucket, String fileName) {
        CopyObjectArgs objectArgs = CopyObjectArgs.builder()
                .bucket(backupBucket).object(newName)
                .source(CopySource.builder().bucket(oldBucket).object(fileName).build())
                .build();
        try {
            minioClient.copyObject(objectArgs);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
